import { useReducer } from 'react';

/**
 * 定义初始状态信息
 */
export interface IstateType {
  materielList: any;
  contractList: any;
  contractMaterList: any;
  addMatrielList: any;
  purchaseList: any;
  contractProcessList: any;
  serverList: any;
}

/**
 * 新增和编辑页面操作枚举
 * @CLEAR_RECEIVE  清空存入 数据
 * @SELECT_MATERIEL 选中的材料数据列表
 *
 */
export enum IAcquisitionActionEnum {
  CLEAR_RECEIVE = 'clearAllReceive',
  SELECT_MATERIEL = 'selectMateriel',
  SELECT_SERVER = 'selectServer',
  ASSIGN_MATERIEL = 'assignMateriel',
  MATERIEL_FORM = 'materielForm',
  CANCEL_MATERIEL = 'cancelMateriel',
  SELECT_CONTRACT = 'selectContract',
  SELECT_PURCHASE = 'selectPurchase',
  SELECT_CONTRACT_PROCESS = 'selectContractProcess',
  ASSIGN_CONTRACT = 'assignContract',
  SELECT_CONTRACT_MATRIEL = 'selectContractMatriel',
  CANCEL_CONTRACT_MATRIEL = 'cancelContractMatriel',
  SELECT_ADD_MATRIEL = 'selectAddMatriel',
}

/**
 * 定义新增和编辑页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
export interface IActionType {
  type: IAcquisitionActionEnum;
  payload?: any;
}

export const useAcquisitionManageReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    materielList: [],
    contractList: [],
    contractMaterList: [],
    addMatrielList: [],
    purchaseList: [],
    contractProcessList: [],
    serverList: [],
  };

  const acquisitionManageReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      /** 选中的材料列表 */
      case IAcquisitionActionEnum.SELECT_MATERIEL:
        return {
          ...state,
          materielList: action?.payload,
        };
      /** 取消选中的材料列表中的某项 */
      case IAcquisitionActionEnum.CANCEL_MATERIEL:
        return {
          ...state,
          materielList: action?.payload,
        };
      // 选中的服务list
      case IAcquisitionActionEnum.SELECT_SERVER:
        return {
          ...state,
          serverList: action?.payload,
        };
      /** 选中的材料信息 */
      case IAcquisitionActionEnum.MATERIEL_FORM:
        return {
          ...state,
          materielForm: action?.payload,
        };
      // 选中的合同
      case IAcquisitionActionEnum.SELECT_CONTRACT:
        return {
          ...state,
          contractList: action?.payload,
        };
      // 选择的采购申请
      case IAcquisitionActionEnum.SELECT_PURCHASE:
        return {
          ...state,
          purchaseList: action?.payload,
        };
      // 选择的履行节点
      case IAcquisitionActionEnum.SELECT_CONTRACT_PROCESS:
        return {
          ...state,
          contractProcessList: action?.payload,
        };
      // 选中合同的物料信息
      case IAcquisitionActionEnum.SELECT_CONTRACT_MATRIEL:
        return {
          ...state,
          contractMaterList: action?.payload,
        };
      // 清空合同的物料信息
      case IAcquisitionActionEnum.CANCEL_CONTRACT_MATRIEL:
        return {
          ...state,
          contractMaterList: [],
        };
      // 自行添加的物料信息
      case IAcquisitionActionEnum.SELECT_ADD_MATRIEL:
        return {
          ...state,
          addMatrielList: action?.payload,
        };
      /**
       *  清空已选数据
       */
      case IAcquisitionActionEnum.CLEAR_RECEIVE:
        return {
          ...state,
          leftSelectRows: [],
          leftSelectKeys: [],
          leftExpandedKeys: [],
          leftAutoExpandParent: false,
          leftSelectName: [],
        };

      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, acquisitionDispatch] = useReducer(acquisitionManageReducer, initState);

  return {
    ...state,
    acquisitionDispatch,
  };
};
